package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.BadgeAchievementDTO;
import com.example.demo.dto.BadgeGrowthDTO;
import com.example.demo.dto.BadgeSpecialDTO;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.StudentAttributeService;
import com.example.demo.service.StudentBadgeService;
import com.example.demo.service.StudentCertificationService;
import com.example.demo.service.StudentTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Lucy
 * @create 2024-02-13 17:04
 */
@RequestMapping("badgeWall")
@Api(value = "徽章墙接口", tags = {"徽章墙接口"})
@RestController
@Slf4j
public class BadgeWallController {

    @Autowired
    private StudentBadgeService service;

    @Autowired
    private StudentAttributeService studentAttributeService;

    @Autowired
    private StudentTaskService studentTaskService;

    @Autowired
    private StudentCertificationService studentCertificationService;
    @ResponseBody
    @ApiOperation("获取徽章墙头部的显示信息")
    @RequestMapping(value = "getBadgeWallHead", method = {RequestMethod.POST,  RequestMethod.GET} )
    public Result<Object> getBadgeWallHead(){
        int studentId = StpUtil.getLoginIdAsInt();
        Student currentStudent = studentCertificationService.selectStudentById(studentId);
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", currentStudent.getNickname());
        String avatar = SystemConstant.aliyunOSSUrlPrefix + currentStudent.getAvatar();
        map.put("avatar", avatar);
        int badgeNumber = service.selectBadgeNumber(studentId);
        map.put("badgeNumber", badgeNumber);
        return Result.ok(map);
    }

    @ResponseBody
    @ApiOperation("获取成就徽章的显示信息")
    @RequestMapping(value = "getAchievementBadge", method = {RequestMethod.POST,  RequestMethod.GET} )
    public Result<List<BadgeAchievementDTO>> getAchievementBadge(){
        int studentId = StpUtil.getLoginIdAsInt();
        List<Badge> badgeList = service.selectTypeBadge(2);
        List<StudentBadge> studentBadgeList = service.selectStudentBadge(studentId);
        List<BadgeAchievementDTO> badgeAchievementDTOList = new ArrayList<>();
        for (Badge badge : badgeList) {
            BadgeAchievementDTO badgeAchievementDTO = new BadgeAchievementDTO();
            badgeAchievementDTO.setBadgeId(badge.getBadgeId());
            badgeAchievementDTO.setName(badge.getName());
            badgeAchievementDTO.setDescription(badge.getDescription());
            String requirement = TaskConstant.badgeAchievementRequirement
                            .replace("task_name", studentTaskService.selectTaskById(badge.getTaskId()).getTaskName());
            badgeAchievementDTO.setRequirement(requirement);
            badgeAchievementDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + badge.getImageUrl());
            badgeAchievementDTO.setIsOwn(0);
            for (StudentBadge studentBadge : studentBadgeList) {
                if (studentBadge.getBadgeId() == badge.getBadgeId()) {
                    badgeAchievementDTO.setIsOwn(1);
                }
            }
            badgeAchievementDTOList.add(badgeAchievementDTO);
        }
        return Result.ok(badgeAchievementDTOList);
    }

    @ResponseBody
    @ApiOperation("获取成长徽章的显示信息")
    @RequestMapping(value = "getGrowthBadge", method = {RequestMethod.POST,  RequestMethod.GET} )
    public Result<List<BadgeGrowthDTO>> getGrowthBadge(){
        int attributeId;
        int attributeValue;
        int studentId = StpUtil.getLoginIdAsInt();
        List<Badge> badgeList = service.selectTypeBadge(1);
        List<StudentAttribute> studentAttributeList = studentAttributeService.getStudentAttributeList(studentId);
        List<BadgeGrowthDTO> badgeGrowthDTOList = new ArrayList<>();
        for(StudentAttribute studentAttribute : studentAttributeList){
            attributeId = studentAttribute.getAttributeId();
            attributeValue = studentAttribute.getValue();
            List<Badge> filteredBadges = new ArrayList<>();
            for (Badge badge : badgeList) {
                if (badge.getAttributeId() == attributeId) {
                    filteredBadges.add(badge);
                }
            }
            Badge minBadge = filteredBadges.stream()
                    .min(Comparator.comparingInt(Badge::getAttributeValue))
                    .orElse(null);
            Badge maxBadge = filteredBadges.stream()
                    .max(Comparator.comparingInt(Badge::getAttributeValue))
                    .orElse(null);
            if(attributeValue < minBadge.getAttributeValue()){
                BadgeGrowthDTO badgeGrowthDTO = new BadgeGrowthDTO();
                badgeGrowthDTO.setBadgeId(minBadge.getBadgeId());
                badgeGrowthDTO.setName(minBadge.getName());
                badgeGrowthDTO.setDescription(minBadge.getDescription());
                String requirement = TaskConstant.badgeGrowthRequirement
                        .replace("attribute_name", studentAttributeService.selectAttributeById(minBadge.getAttributeId()).getName())
                        .replace("attribute_value", Integer.toString(minBadge.getAttributeValue()));
                badgeGrowthDTO.setRequirement(requirement);
                badgeGrowthDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + minBadge.getImageUrl());
                badgeGrowthDTO.setIsOwn(0);
                badgeGrowthDTO.setAttributeValue(attributeValue);
                badgeGrowthDTOList.add(badgeGrowthDTO);
                continue;
            }
            if(attributeValue > maxBadge.getAttributeValue()){
                BadgeGrowthDTO badgeGrowthDTO = new BadgeGrowthDTO();
                badgeGrowthDTO.setBadgeId(maxBadge.getBadgeId());
                badgeGrowthDTO.setName(maxBadge.getName());
                badgeGrowthDTO.setDescription(maxBadge.getDescription());
                String requirement = TaskConstant.badgeGrowthRequirement
                        .replace("attribute_name", studentAttributeService.selectAttributeById(maxBadge.getAttributeId()).getName())
                        .replace("attribute_value", Integer.toString(maxBadge.getAttributeValue()));
                badgeGrowthDTO.setRequirement(requirement);
                badgeGrowthDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + maxBadge.getImageUrl());
                badgeGrowthDTO.setIsOwn(1);
                badgeGrowthDTO.setAttributeValue(attributeValue);
                badgeGrowthDTOList.add(badgeGrowthDTO);
                continue;
            }
            filteredBadges.sort(Comparator.comparingInt(Badge::getAttributeValue));
            Badge prevBadge = new Badge();
            for(Badge badge : filteredBadges){
                if(attributeValue < badge.getAttributeValue()){
                    BadgeGrowthDTO badgeGrowthDTO = new BadgeGrowthDTO();
                    badgeGrowthDTO.setBadgeId(prevBadge.getBadgeId());
                    badgeGrowthDTO.setName(prevBadge.getName());
                    badgeGrowthDTO.setDescription(prevBadge.getDescription());
                    String requirement = TaskConstant.badgeGrowthRequirement
                            .replace("attribute_name", studentAttributeService.selectAttributeById(prevBadge.getAttributeId()).getName())
                            .replace("attribute_value", Integer.toString(prevBadge.getAttributeValue()));
                    badgeGrowthDTO.setRequirement(requirement);
                    badgeGrowthDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + prevBadge.getImageUrl());
                    badgeGrowthDTO.setIsOwn(1);
                    badgeGrowthDTO.setAttributeValue(attributeValue);
                    badgeGrowthDTOList.add(badgeGrowthDTO);
                    break;
                }
                prevBadge = badge;
            }
        }
        return Result.ok(badgeGrowthDTOList);
    }

    @ResponseBody
    @ApiOperation("获取特殊徽章的显示信息")
    @RequestMapping(value = "getSpecialBadge", method = {RequestMethod.POST,  RequestMethod.GET} )
    public Result<List<BadgeSpecialDTO>> getSpecialBadge(){
        int studentId = StpUtil.getLoginIdAsInt();
        int badgeNumber = service.selectBadgeNumber(studentId);
        List<Badge> badgeList = service.selectTypeBadge(3);
        List<StudentBadge> studentBadgeList = service.selectStudentBadge(studentId);
        List<BadgeSpecialDTO> badgeSpecialDTOList = new ArrayList<>();
        badgeList.sort(Comparator.comparingInt(Badge::getBadgeNumber));
        for (Badge badge : badgeList) {
            BadgeSpecialDTO badgeSpecialDTO = new BadgeSpecialDTO();
            badgeSpecialDTO.setBadgeId(badge.getBadgeId());
            badgeSpecialDTO.setName(badge.getName());
            badgeSpecialDTO.setDescription(badge.getDescription());
            badgeSpecialDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + badge.getImageUrl());
            String requirement = TaskConstant.badgeSpecialRequirement
                    .replace("badge_number", Integer.toString(badge.getBadgeNumber()));
            badgeSpecialDTO.setRequirement(requirement);
            badgeSpecialDTO.setBadgeNumber(badgeNumber);
            badgeSpecialDTO.setProgressValue(badge.getBadgeNumber());
            badgeSpecialDTO.setIsOwn(0);
            for (StudentBadge studentBadge : studentBadgeList) {
                if (studentBadge.getBadgeId() == badge.getBadgeId()) {
                    badgeSpecialDTO.setIsOwn(1);
                }
            }
            badgeSpecialDTOList.add(badgeSpecialDTO);
        }
        return Result.ok(badgeSpecialDTOList);
    }

    @ResponseBody
    @ApiOperation("成长徽章详情页显示接口")
    @RequestMapping(value = "getBadgeGrowthInfo/{badgeId}")
    public Result<List<BadgeGrowthDTO>> getBadgeGrowthInfo(@PathVariable("badgeId")int badgeId) {
        int studentId = StpUtil.getLoginIdAsInt();
        Badge currentBadge = service.selectBadgeById(badgeId);
        List<Badge> badgeList = service.selectBadgeByAttributeId(currentBadge.getAttributeId());
        List<BadgeGrowthDTO> badgeGrowthDTOList = new ArrayList<>();
        for(Badge badge : badgeList){
            BadgeGrowthDTO badgeGrowthDTO = new BadgeGrowthDTO();
            badgeGrowthDTO.setBadgeId(badge.getBadgeId());
            badgeGrowthDTO.setName(badge.getName());
            badgeGrowthDTO.setDescription(badge.getDescription());
            String requirement = TaskConstant.badgeGrowthRequirement
                    .replace("attribute_name", studentAttributeService.selectAttributeById(badge.getAttributeId()).getName())
                    .replace("attribute_value", Integer.toString(badge.getAttributeValue()));
            badgeGrowthDTO.setRequirement(requirement);
            badgeGrowthDTO.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + badge.getImageUrl());
            badgeGrowthDTO.setIsOwn(1);
            StudentBadge studentBadge = service.selectStudentBadge(studentId, badge.getBadgeId());
            if(studentBadge == null) badgeGrowthDTO.setIsOwn(0);
            badgeGrowthDTOList.add(badgeGrowthDTO);
        }
        return Result.ok(badgeGrowthDTOList);
    }
}
